/**
 * An interface which allows a driver to login and see all their
 * information
 * @author Adam Higginson
 * @version 0.1
 */
 
//Use this classpath to connect the database when not using an IDE
//CLASSPATH=$CLASSPATH:~/mysql-connector-java-5.1.12-bin.jar

import java.awt.GridLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JButton;



public class DriverInterface extends JFrame													 implements ActionListener
{
        //The id number of the driver
        private int driverID;
        //The name of the driver
        private String driverName;

        //A label holding the name of the driver
        private JLabel nameLabel;
        //A label holding the no of holidays left for this driver
        private JLabel holidayLabel;
        //Button for requesting a holiday
        private JButton requestHolidayButton;
	
	
	public DriverInterface(int driverID)
	{
            this.driverID = driverID;
            this.driverName = DriverInfo.getName(driverID);

            setTitle("Driver home for: " + this.driverName);
            //setResizable(false);

            int noHolidays = DriverInfo.getHolidaysTaken(this.driverID);
            this.nameLabel = new JLabel("Welcome " + this.driverName);
            this.holidayLabel = new JLabel("You have had: " + noHolidays + " holidays.");
            this.requestHolidayButton = new JButton("Request Holiday");

            Container contents = getContentPane();
            contents.setLayout(new GridLayout(3, 0));

            contents.add(this.nameLabel);
            contents.add(this.holidayLabel);
            contents.add(this.requestHolidayButton);
            requestHolidayButton.addActionListener(this);

            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            pack();
            
	}
	
		
	public void actionPerformed(ActionEvent event)
	{
            System.out.println("Creating Holiday Interface");
            HolidayInterface hinterface = new HolidayInterface(this.driverID);
            hinterface.setVisible(true);
	}

         @Override
         public void dispose()
         {
            System.out.println("Closing");
            database.busDatabase.close();
            super.dispose();
         }
	
	public static void main(String[] args)
	{
		//Open up database
		database.openBusDatabase();
		//The received ID
		int getID = -1;
		
		boolean isValidDriver = false;
		//Loop while input is not correct
		while (!isValidDriver)
		{
                    //Display welcome popup
                    String input = JOptionPane.showInputDialog
                                                            (null, "Enter ID",
                                                            "Enter driver number: ",
                                                            JOptionPane.INFORMATION_MESSAGE);

                    //Look for driver with given ID
                    //getID = DriverInfo.findDriver(input);

                    getID = Integer.parseInt(input);

                    if (input == null)
                    {
                        database.busDatabase.close();
                        System.exit(0);
                    }

                    if (getID == 0)
                    {
                        ErrorGui.showError("Error", "Could not find driver with that number!");
                    }
                    else
                        isValidDriver = true;
		}
		
		//Create driver interface
		DriverInterface driverInterface = new DriverInterface(getID);
		driverInterface.setVisible(true);
	}

}
